package com.qyer.search.sort;

import com.google.common.collect.Lists;
import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.model.aggregation.ProductProject;
import com.qyer.search.model.lastminute.VSKU;
import com.qyer.search.model.mutable.IndexMap;
import com.qyer.search.model.result.Type1SearchEntity;
import com.qyer.search.model.sort.SortEntity;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by tangzheng on 16/8/18.
 */
public class VSalesCacheSorter {
  private List<Integer> sourceList;
  private int from;
  private int to;
  private int size;
  private int type;

  private List<VSKU> sortEntityList;

  public VSalesCacheSorter(List<Integer> sourceList, int from, int to) {
    this.sourceList = sourceList;
    this.from = from;
    this.to = to;
    this.size = sourceList.size();
  }

  public VSalesCacheSorter(List<Integer> sourceList, int type) {
    this.sourceList = sourceList;
    this.size = sourceList.size();
    this.type = type;
    this.sortEntityList = Lists.newArrayListWithCapacity(sourceList.size());
  }

  public void sort(IndexMap<ProductProject> product_image){
    Map<Integer,Long> sales = MutableCaches.getInstance().SALES.getCacheContent();
    List<SortEntity> list = Lists.newArrayListWithCapacity(this.size);
    for(int i = 0; i < this.size; i++){
      int id = sourceList.get(i);
      long factor = Integer.MIN_VALUE;
      if(sales.containsKey(id)){
        factor = sales.get(id);
      }
      SortEntity sortEntity = new SortEntity(id, factor);
      list.add(sortEntity);
    }
    Collections.sort(list);
    for(SortEntity sortEntity : list){
      int id = sortEntity.getId();
      if(product_image.containsKey(id)){
        ProductProject productProject = product_image.get(id);
        //VSKU vsku = new VSKU();
      }
    }
  }

  public List<Type1SearchEntity> sort(int from, int to){return null;}

  public List<Integer> getSourceList() {
    return sourceList;
  }

  public void setSourceList(List<Integer> sourceList) {
    this.sourceList = sourceList;
  }

  public int getFrom() {
    return from;
  }

  public void setFrom(int from) {
    this.from = from;
  }

  public int getTo() {
    return to;
  }

  public void setTo(int to) {
    this.to = to;
  }

  @Override public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;

    VSalesCacheSorter that = (VSalesCacheSorter) o;

    if (from != that.from)
      return false;
    if (to != that.to)
      return false;
    if (size != that.size)
      return false;
    if (!sourceList.equals(that.sourceList))
      return false;
    return sortEntityList.equals(that.sortEntityList);

  }

  @Override public int hashCode() {
    int result = sourceList.hashCode();
    result = 31 * result + from;
    result = 31 * result + to;
    result = 31 * result + size;
    result = 31 * result + sortEntityList.hashCode();
    return result;
  }

  @Override public String toString() {
    return "VSalesCacheSorter{" +
      "sourceList=" + sourceList +
      ", from=" + from +
      ", to=" + to +
      ", size=" + size +
      ", sortEntityList=" + sortEntityList +
      '}';
  }
}
